// 58. Length of Last Word
// Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

// If the last word does not exist, return 0.

// Note: A word is defined as a character sequence consists of non-space characters only.

// For example,
// Given s = "Hello World",
// return 5.

package main

func lengthOfLastWord(s string) int {
	count := len(s) - 1
	if count < 0 {
		return 0
	}

	for i := count; i >= 0; i-- {
		if s[i] == ' ' {
			if i == count {
				count--
				continue
			}
			return count - i
		}
	}

	return count + 1
}

func main() {
	s, n := "Hello World", 5
	c := lengthOfLastWord(s)
	println(c, "=", n, c == n)

	s, n = "Hello fuck", 4
	c = lengthOfLastWord(s)
	println(c, "=", n, c == n)

	s, n = "   ", 0
	c = lengthOfLastWord(s)
	println(c, "=", n, c == n)
}
